<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="登录地址" prop="ipaddr">
        <el-input
          v-model="queryParams.ipaddr"
          placeholder="请输入登录地址"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-if="dict.sys_common_status"
          v-model="queryParams.status"
          placeholder="登录状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dd in dict.sys_common_status.dictData"
            :key="dd.dictCode"
            :label="dd.dictLabel"
            :value="dd.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="登录时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
        >清空</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <!--列表-->
    <el-table ref="tables" :data="list" :default-sort="defaultSort" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="访问编号" align="center" prop="infoId" />
      <el-table-column label="用户名称" align="center" prop="userName" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']" />
      <el-table-column label="登录地址" align="center" prop="ipaddr" width="130" :show-overflow-tooltip="true" />
      <el-table-column label="登录地点" align="center" prop="loginLocation" :show-overflow-tooltip="true" />
      <el-table-column label="浏览器" align="center" prop="browser" :show-overflow-tooltip="true" />
      <el-table-column label="操作系统" align="center" prop="os" />
      <el-table-column label="登录状态" align="center" prop="status">
        <!--        <template slot-scope="scope">-->
        <!--          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>-->
        <!--        </template>-->
        <template slot-scope="scope">
          <div v-if="scope.row.status == 0">成功</div>
          <div v-else-if="scope.row.status == 1">失败</div>
        </template>
      </el-table-column>
      <el-table-column label="操作信息" align="center" prop="msg" />
      <el-table-column label="登录日期" align="center" prop="loginTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.loginTime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
      :current-page="current"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

</template>

<script>

import qs from 'qs'
import axios from 'axios'

export default {
  name: 'Logininfor',
  data() {
    return {
      dicts: ['sys_common_status'],
      dict: {},
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: '', order: '' },
      // 查询参数
      queryParams: {},
      current: 1,
      size: 10
    }
  },
  created() {
    this.getList(this.current, this.size)
    this.getDictData()
  },
  methods: {
    /** 查询字典 */
    getDictData() {
      this.axios.get('http://localhost:8080/dict/maps/' + this.dicts.join())
        .then(res => {
          this.dict = res.data
        })
    },
    /** 查询登录日志列表 */
    getList(current, size) {
      axios.post('http://localhost:8080/intelligence_community/sys-logininfor/loginLogList?current=' + current + '&size=' + size + '&dateRange=' + this.dateRange.join(), this.queryParams)
        .then(res => {
          this.list = res.data.records
          this.current = res.data.current
          this.total = res.data.total
        })
    },
    /** 搜索按钮操作 */
    handleQuery(current, size) {
      current = 1
      size = this.size
      this.getList(current, size)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.current = 1
      this.dateRange = []
      this.queryParams = {}
      // this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.$refs.tables.clearSort()
      this.handleQuery()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.infoId)
      this.multiple = !selection.length
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop
      this.queryParams.isAsc = column.order
      this.getList(this.current, this.size)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.infoId || this.ids
      this.$messagebox.confirm('是否确认删除访问编号为"' + infoIds + '"的数据项？').then(() => {
        axios.delete('http://localhost:8080/intelligence_community/sys-logininfor/loginLogDelete/' + infoIds)
          .then(res => {
            if (res.data == '删除成功') {
              this.$message.warning('删除成功')
              this.getList(this.current, this.size)
            } else {
              this.$message.warning('删除失败')
            }
          })
      }).catch(() => {})
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$messagebox.confirm('是否确认清空所有登录日志数据项？').then(function() {
        axios.get('http://localhost:8080/intelligence_community/sys-logininfor/emptyDatabase')
      }).then(() => {
        location.reload()
        this.$message.warning('清空成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      axios({
        method: 'get',
        url: 'http://localhost:8080/intelligence_community/sys-logininfor/downloadExcel',
        responseType: 'arraybuffer'
      })
        .then(res => {
          // 使用Blob对象将后端返回的Arraybuffer格式得数据转成blob类型的数据 window.URL.createObjectURL(blob)可以把一个blob转成blobURL，用作图片显示，文件下载（不能再服务器上存储，只能在浏览器使用）
          const b = new Blob([res.data], { type: 'application/vnd.ms-excel' })
          // 使用URL对象将blob类型的数据生成一个在线下载的链接
          const url = URL.createObjectURL(b)
          // 然后我们使用a标签进行下载
          const link = document.createElement('a')

          // 设置导出的文件名
          link.download = '登入日志'
          link.href = url
          // 点击获取文件
          link.click();

          (err) => {
            console.log(err)
          }
        })
    },

    //  分页
    handleSizeChange(val) {
      this.size = val
      this.getList(this.current, val)
    },
    handleCurrentChange(val) {
      this.getList(val, this.size)
    }
  }
}
</script>

<style scoped>

</style>
